import java.io.*;
import java.net.*;

class connectToVM
{
 	public static void main(String args[]) throws Exception
 	{
		int pid=5000;
		if(args.length>0) {
			try {
				pid = Integer.parseInt(args[0]);
			} catch (NumberFormatException e) {
				System.err.println("Argument" + " must be an integer");
				System.exit(1);
			}
		}
		

		try {

		  	String answer;
			Socket clientSocket;
			ObjectOutputStream outToBroker;
			ObjectInputStream inFromBroker;
			
			BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
			
		  	//get client request for vmid
		  	System.out.println( "Please specify the vmid:" );
		  	Integer vmid=Integer.parseInt(inFromUser.readLine());

			//connect to broker to request connection to the vm
			clientSocket = new Socket("localhost", pid);
			outToBroker = new ObjectOutputStream(clientSocket.getOutputStream());
			inFromBroker = new ObjectInputStream(clientSocket.getInputStream());
		  	outToBroker.writeObject(new String("ConnectVm"));
		  	outToBroker.writeObject(vmid);
		  	answer = (String) inFromBroker.readObject();
			outToBroker.close();
			inFromBroker.close();
 		  	clientSocket.close();

			String [] s=answer.split("\\?");
			if(s.length>1) {
				int serverId=Integer.valueOf(s[0]);
				//make connection to the vm
				clientSocket = new Socket("localhost", serverId);
			  	DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
			  	BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
			  	
			  	String input="Hello?"+s[1]+"?"+s[2];
			  	outToServer.writeBytes(input+'\n');
	      		answer = inFromServer.readLine();			  	
			  	printStream(answer);
	      		
			  	boolean connected=true; 
			  	while (connected) {
					input = inFromUser.readLine();
					outToServer.writeBytes(input + '\n');
		      		answer = inFromServer.readLine();
		      		if(answer.equals("Goodbye")) 
		      			connected=false;
				  	printStream(answer);
				}			  					  	
				
			  	outToServer.close();
			  	inFromServer.close();
			  	clientSocket.close();
			  	
			  	//connect to the broker to make the vm inactive for future checkouts
				input="DisconnectVm?";
				clientSocket = new Socket("localhost", pid);
			  	outToBroker = new ObjectOutputStream(clientSocket.getOutputStream());
			  	inFromBroker = new ObjectInputStream(clientSocket.getInputStream());
			  	outToBroker.writeObject(new String("DisconnectVm"));
			  	outToBroker.writeObject(vmid);
			  	answer = (String) inFromBroker.readObject();
			  	System.out.println(answer);			      					  	
				outToServer.close();
				inFromServer.close();
	 		  	clientSocket.close();
			} else 
				System.out.println(answer);
		} catch (ConnectException e) {
	        	System.err.println("pid" + " must refer to a valid socket");
	        	System.exit(1);
		}				

 	}
 	public static void printStream(String answer)
 	{
 		answer=answer.replaceAll("#", "\n");
 		System.out.println(answer);

 	}

}
